package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.pojo.BookStockVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.BookStockDO;
import com.jinzhi.jzweb.service.BookStockService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 库存书籍管理主表
 * </pre>
 * <small> 2021-04-08 09:00:19 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/bookStock")
public class BookStockController extends BaseController {
	@Autowired
	private BookStockService bookStockService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:bookStock:bookStock")
	String BookStock(){
	    return "jzweb/bookStock/bookStock";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:bookStock:bookStock")
	public Result<Page<BookStockDO>> list(BookStockDO bookStockDTO){
		// 查询列表数据
		Page<BookStockDO> page = bookStockService.selectPage(getPage(BookStockDO.class),
					bookStockService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				BookStockDO obj = (BookStockDO) page.getRecords().get(i);
			BookStockVo bookStockVo=new BookStockVo();
			bookStockVo.setBookType(obj.getBookType());

			//查询书籍数量
			Map map=new HashMap();
			map.put("bookId",obj.getId());
			Integer numbook=bookStockService.bookNum(map);
			//查询书籍数量
			Map map1=new HashMap();
			map1.put("name",obj.getBookTypes());
			//查询销量
			Map maostock=bookStockService.integerSales(map1);
			Integer integerSales=0;


			bookStockVo.setCteateTime(obj.getCteateTime());
			bookStockVo.setMoney(obj.getMoney());
			bookStockVo.setWarehouse(obj.getWarehouse());
			bookStockVo.setId(obj.getId());
			bookStockVo.setCreateUser(obj.getCreateUser());
			System.out.println("书籍列别========="+obj.getBookType());
			//销量
			if(obj.getBookType().indexOf("基础知识")!=-1){
				integerSales=Integer.valueOf(maostock.get("num1").toString());
			}else if(obj.getBookType().indexOf("初级")!=-1){
				integerSales=Integer.valueOf(maostock.get("num2").toString());
			}else if(obj.getBookType().indexOf("中级")!=-1){
				integerSales=Integer.valueOf(maostock.get("num3").toString());
			}else if(obj.getBookType().indexOf("高级")!=-1){
				integerSales=Integer.valueOf(maostock.get("num4").toString());
			}
			int stockNum=numbook-integerSales;
			bookStockVo.setSalesVolume(integerSales);
			//总计数量
			bookStockVo.setTotal(numbook);
			//库存
			bookStockVo.setStockNum(stockNum);
			bookStockVo.setBookTypes(obj.getBookTypes());
			list.add(bookStockVo);

		}

		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:bookStock:add")
	String add(Model model){
		List <Map> mapList=new ArrayList<>();

		Map map1=new HashMap();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);
	    return "jzweb/bookStock/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:bookStock:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BookStockDO bookStock = bookStockService.selectById(id);
		model.addAttribute("bookStock", bookStock);

		List <Map> mapList=new ArrayList<>();

		Map map1=new HashMap();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);
	    return "jzweb/bookStock/edit";
	}
	
	@Log("添加库存书籍管理主表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:bookStock:add")
	public Result<String> save( BookStockDO bookStock){
		bookStock.setCreateUser(this.getUser().getUsername());
		bookStock.setCteateTime(new Date());
		bookStock.setCreateUserId(this.getUserId());
		bookStockService.insert(bookStock);
        return Result.ok();
	}
	
	@Log("修改库存书籍管理主表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:bookStock:edit")
	public Result<String>  update( BookStockDO bookStock){
		boolean update = bookStockService.updateById(bookStock);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除库存书籍管理主表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:bookStock:remove")
	public Result<String>  remove( Long id){
		bookStockService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除库存书籍管理主表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:bookStock:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		bookStockService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
